<template>
  <div class="app-container">
    <!-- 查询 -->
    <tl-list-search ref="search" :query-param="queryParam" @search="doQuery" :isDisplayDefaultSearch="false" :isDisplaySearch="false">
      <template slot="search">
        <el-form-item label="项目" v-if="editFlag">
          <el-input v-model="queryParam.qry_dispprojectid" :disabled="true" class="tl-ref-selector" style="width:300px" @change="changedispprojectid">
            <el-button slot="append" type="primary" icon="el-icon-search" @click="openpmProjectRefDialog"></el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="日期" prop="qry_begindocdate">
          <el-date-picker v-model="queryParam.qry_dates" type="daterange" value-format="yyyy-MM-dd" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions" @change="changedates" />
          <!-- <el-date-picker v-model="queryParam.qry_begindocdate" type="date" style="width:150px" value-format="yyyy-MM-dd" placeholder="开始时间" />至
          <el-date-picker v-model="queryParam.qry_enddocdate" type="date" style="width:150px" value-format="yyyy-MM-dd" placeholder="结束时间" /> -->
        </el-form-item>
        <el-button class="filter-item" type="primary" icon="el-icon-search" @click="doQuery">搜索</el-button>
        <el-button class="filter-item" type="primary" icon="el-icon-refresh-right" @click="resetQueryParam" v-if="editFlag" :disabled="isreset">重置</el-button>
      </template>
    </tl-list-search>
    <div>
      <!--表格渲染-->
      <el-table class="tl-table" ref="mainTable" :height="tableHeight" resizable-key="tableHeight" :resizable-refs="tableResizeRefs" v-loading="loading" :data="datas" @selection-change="doSelectChange" @sort-change="doSortChange" row-key="id" border fit :row-class-name="doChangeRowClassName">
        <el-table-column type="index" label="序号" width="55px" align="center"></el-table-column>
        <el-table-column prop="calendardate" label="日期" width="120px" sortable="custom" sort-by="calendardate" align="center" header-align="center" :formatter="dateFormat" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column v-for="(item, index) in userList" :key="item + index" :label="item" width="200px" align="center" header-align="center">
          <template slot-scope="scope">
            <img :src="scope.row.userProjectList[index].icon" align="center" />
          </template>
        </el-table-column>
      </el-table>
      <!-- 列表分页 -->
      <el-row class="pagebar" ref="pager">
        <el-pagination @size-change="doChangeSize" @current-change="doChangeIndex" :current-page="pageInfo.currentPage" :page-sizes="pageInfo.pageSizes" :total="pageInfo.totalCount" layout="total, sizes, prev, pager, next, jumper" background class="tl-table-pagination"> </el-pagination>
      </el-row>
      <tlpm-project-ref ref="pmProjectRef" @confirm="getpmProjectRefData" :isSingleSelect="true"></tlpm-project-ref>
    </div>
  </div>
</template>

<script>
/** mixins */
import { mixin_list } from '@tapui/mixins/mixin_list'
/** 子组件 */
import tlpmProjectRef from '@/views/ProjectManage/pmProject/highestAuthorityRefList.vue'
import tlpmTeamRef from '@/views/ProjectManage/pmTeam/refList.vue'
import { validateNull } from '@tapui/utils/validate'
/** 工具类 */
import { getAction, putAction, postAction, deleteAction } from '@tapui/utils/http'
export default {
  name: 'taskProjectForPerson',
  mixins: [mixin_list],
  components: { tlpmProjectRef, tlpmTeamRef },
  data() {
    return {
      description: '项目工作汇总',
      urlPrefix: '/pm/taskLog/',
      url: {
        queryCodeList: '/pm/taskLog/dataCodeList',
        exportExcel: '/pm/taskLog/exportTaskLogProjectExcel',
        projectForPersonStatistics: '/pm/taskLog/projectForPersonStatistics'
      },
      // 分页信息
      pageInfo: {
        currentPage: 1, // 当前页面
        pageSize: 50, // 每页记录数
        pageSizes: ['50', '100'],
        totalCount: 0 // 总数
      },
      isreset: false,
      // 排序参数
      sorterInfo: {
        sidx: 'calendardate',
        sord: 'asc'
      },
      isQueryOnCreated: false,
      userList: [],
      pickerOptions: {
        shortcuts: [
          {
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            }
          }
        ]
      },
      // 当前页面的编辑状态
      editFlag: true
    }
  },
  props: {
    projectId: {
      type: [Number, String],
      default: function() {
        return ''
      }
    },
    // 模块是否可以编辑
    moduleEditFlag: {
      type: Number,
      default: function() {
        return 1
      }
    },
    // 当前登录人角色 PM--项目经理 PP--项目参数人 OTHER--其他
    currentRole: {
      type: String,
      default: function() {
        return 'PM'
      }
    }
  },
  methods: {
    changedispprojectid() {
      if (JSON.stringify(this.queryParam) !== '{}' && this.queryParam.qry_dispprojectid !== '') {
        this.isreset = false
      } else {
        this.isreset = true
      }
    },
    changedates() {
      if (JSON.stringify(this.queryParam) !== '{}' && this.queryParam.qry_dates !== '') {
        this.isreset = false
      } else {
        this.isreset = true
      }
    },
    // 禁用表格前面的选择
    doDisableCheckBox(row, index) {
      if (row.state !== 0 && row.state !== 2) {
        return false
      } else {
        return true
      }
    },
    /** 打开项目参照 */
    openpmProjectRefDialog() {
      this.$refs.pmProjectRef.visible = true
    },
    /** 获取项目参数数据 */
    getpmProjectRefData(selectionRow, selectKey, selectionValue) {
      const { id, name } = selectionRow
      this.$set(this.queryParam, 'qry_projectid', id)
      this.$set(this.queryParam, 'qry_dispprojectid', name)
      this.loadData({ currentPage: 1 })
    },
    resetQueryParam() {
      this.queryParam = {}
      this.changedispprojectid()
      this.changedates()
    },
    initParam() {
      this.editFlag = this.moduleEditFlag === 1 && this.currentRole === 'PM'
      this.reduceHeight = this.editFlag ? 134 : 325
      this.resizeHandler()
      this.$set(this.queryParam, 'qry_projectid', this.projectId)
      this.doQuery()
    },
    getUserList(arr) {
      this.userList = {}
      if (arr.length > 0) {
        var userProjectList = arr[0].userProjectList
        for (var j = 0, len = userProjectList.length; j < len; j++) {
          this.userList[j] = userProjectList[j].username
        }
      }
    },
    beforeLoadData() {
      const { qry_dates, qry_projectid, qry_dispprojectid } = this.queryParam
      const param = {
        qry_projectid,
        qry_dispprojectid
      }
      if (qry_dates) {
        this.queryParam = {
          ...param,
          qry_begindocdate: qry_dates[0],
          qry_enddocdate: qry_dates[1]
        }
      } else {
        this.queryParam = param
      }
      return true
    },
    afterLoadData() {
      debugger
      this.isreset = false
      if (typeof this.queryParam.qry_dates === 'undefined' && typeof this.queryParam.qry_dispprojectid === 'undefined') {
        this.isreset = true
      }
      if (this.queryParam.qry_begindocdate && this.queryParam.qry_enddocdate) {
        this.queryParam = {
          ...this.queryParam,
          qry_dates: [this.queryParam.qry_begindocdate, this.queryParam.qry_enddocdate]
        }
      }
    },
    /** 列表数据加载开始 */
    loadData(paramMap) {
      if (!this.url.projectForPersonStatistics) {
        this.$tap.alert('请设置projectForPersonStatistics地址!', 'warning')
        return
      }
      // 得到加载分页
      if (validateNull(paramMap) === false && Object.keys(paramMap).indexOf('currentPage') > -1) {
        this.pageInfo.currentPage = paramMap['currentPage']
      }
      if (!this.queryParam.qry_projectid) {
        this.$tap.alert('请先选择项目')
        return
      }
      if (!this.beforeLoadData()) return
      // 查询条件
      var params = this.getQueryParams()
      // 数据查询
      this.loading = true
      getAction(this.url.projectForPersonStatistics, params)
        .then(res => {
          if (res.success) {
            this.datas = res.result.dataList
            this.getUserList(this.datas)
            this.pageInfo.totalCount = res.result.totalCount
          } else {
            this.$tap.alert(res.message, 'warning')
          }
          this.afterLoadData()
          this.loading = false
        })
        .catch(err => {
          this.loading = false
        })
    }
  },
  mounted() { }
}
</script>

<style scoped></style>
